#include "memofm.h"
//---------------------------------------------------------------------------

SMemo::SMemo(QWidget *Parent) : QTextEdit(Parent)
{
    setAcceptRichText(false);
}

void SMemo::keyPressEvent(QKeyEvent *e)
{
    int k = e->key();
    Qt::KeyboardModifiers m = e->modifiers();
    if (m==Qt::ControlModifier && (k==Qt::Key_Enter || k==Qt::Key_Return))
    {
        emit onCtrlEnter();
    }
    else
    {
        //Beep(10000, 25);
        QTextEdit::keyPressEvent(e);
    }
}
//---------------------------------------------------------------------------

SMemoForm::SMemoForm(QWidget *Parent) :
        QWidget(Parent, Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::Window)
{
    setGeometry(0, 0, 0, 0);
    QFont font;
    font.setFamily("Calibri");
    font.setPointSize(11);
    minh = 28;
    Memo = new SMemo(this);
    Memo->setFont(font);
    Memo->show();
    connect(Memo, SIGNAL(onCtrlEnter()), this, SLOT(CtrlEnterTrap()));
    Log = new QTextEdit(this);
    Log->setReadOnly(true);
    Log->setFont(font);
    Log->show();
}

SMemoForm::~SMemoForm()
{
    delete Memo;
    delete Log;
}

void SMemoForm::resizeEvent(QResizeEvent *e)
{
    QSize sz = e->size();
    int w = sz.width();
    int h = sz.height();

    int h1 = h;
    if (!Log->toPlainText().isEmpty())
    {
        h1 = h*30/100; // memo_height: 70%;
        if (h1 < minh)
            h1 = minh;
    }

    Log->setGeometry(0, 0, w, h-h1);
    Memo->setGeometry(0, h-h1, w, h1);
}

void SMemoForm::acceptMessage(QString name, QString text, bool in)
{
    QString color = in? "red": "blue";
    QString html = "<p><font style='color:"+color+"; font-size:9pt; font-weight:bold;'>";
    html += name;
    html += "</font><br>";
    html += text.replace("\n", "<br>");
    html += "</p>";
    Log->append(html);
    QResizeEvent e(size(), size());
    resizeEvent(&e);
}

QString SMemoForm::retrieveMessage()
{
    QString res = Memo->toPlainText();
    Memo->clear();
    return res;
}

bool SMemoForm::isReadyToSend()
{
    return !Memo->document()->isEmpty();
}

void SMemoForm::CtrlEnterTrap()
{
    emit onCtrlEnter();
}
//--------------------------------------------------------------------------------


